<?php

namespace app\admin\services;

use think\Service;
use think\facade\Request;
use app\admin\model\ManualGroupModel;

/**
 * @手册分类管理
 */
class ManualGroupService extends Service
{
    protected $manualGroupModel;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->manualGroupModel = new ManualGroupModel();
    }
    
    /**
     * @保存数据
     */
    public function saveData(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        
        $where=[];
        empty($post['id'])?$where=[]:$where=['id'=>$post['id']];
        
        if(empty($post['title'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        if(empty($post['sort']) && $post['sort'] != '0'){
            return $this->_getResponse("PARAM_EORRE");
        }
        if(empty($post['status'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        
        if(empty($post['pid']) && $post['pid']!='0'){
            return $this->_getResponse("PARAM_EORRE");
        }
       
        $data = $this->manualGroupModel->saveData($post,$where);
        
        if(!$data){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("ENBES_DATE");
    }
    
    /**
     * @修改状态
     * @param int $id 数据ID
     */
    public function editStatus(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        
        $where=[];
        empty($post['id'])?$where=[]:$where=['id'=>$post['id']];
        
        if(empty($post['status'])){
            return $this->_getResponse("PARAM_EORRE");
        }
       
        $data = $this->manualGroupModel->saveData($post,$where);
        
        if(!$data){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("ENBES_DATE");
    }
    
    /**
     * @获取列表分页数据
     * @param int $pageIndex 当前页码
     * @param int $pageSize 每页条数
     * @param string $keyword 关键字查询
     * @param int $groupId 分类ID
     */
    public function getListByPage(){
        $post = Request::param();
        $page = isset($post['pageIndex'])?$post['pageIndex']:1;
        $limit = isset($post['pageSize'])?$post['pageSize']:10;
        if(!empty($post['title'])){
            $where[]=['title','like','%'.$post['title'].'%'];
        }else{
            $where[]=['status','neq',-1];
        }
        
        if(!empty($post['pid']) || $post['pid']=='0'){
            $where[]=['pid','eq',$post['pid']];
        }
        $ruslt = $this->manualGroupModel->selectDataByPage($page,$limit,$where);

        $counts = $this->manualGroupModel->getCount($where);
        
        $data['rel'] = true;
        $data['msg'] = "获取成功";
        $data['list'] = $ruslt;
        $data['count'] = $counts;
        
        return $data;
    }
    
    /**
     * @获取所有数据
     */
    public function getDataAll(){
        $where[]=['status','neq',-1];
        $ruslt = $this->manualGroupModel->selectListData($where);
        return $ruslt;
    }
    
    /**
     * @获取顶级数据
     */
    public function getParentData($where=[]){
        if(empty($where)){
            $where=[['pid','eq',0],['status','neq',-1]];
        }
        $ruslt = $this->manualGroupModel->selectListData($where);
        return $ruslt;
    }
    
    /**
     * @删除
     * @param int $id ID
     */
    public function del(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        
        $where=[];
        if(empty($post['id'])){
            return $this->_getResponse("PARAM_EORRE");
        }else{
            $where=['id'=>$post['id']];
        }
        
        $data = $this->manualGroupModel->del($where);
        
        if(!$data){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        
        return $this->_getResponse("DELETE_DATE");
    }

    /**
     * @param $key
     * @param array $data
     * @return mixed
     */
    protected function _getResponse($key, $data = [])
    {
        $maps = [
            "SUCCESS" => ["status" => 0, "msg" => "sccuess", "data" => $data],
            "USER_ADD" => ["status" => 0, "msg" => "添加成功", "data" => $data],
            "ENBES_DATE" => ["status" => 0, "msg" => "操作成功", "data" => $data],
            "DELETE_DATE" => ["status" => 0, "msg" => "删除成功", "data" => $data],
            "PARAM_EORRE" => ["status" => 1, "msg" => "参数错误", "data" => $data],
            "NUKNOW_EORRE" => ["status" => 2, "msg" => "未知错误", "data" => $data],
            "AUTH_EORRE" => ["status" => 3, "msg" => "权限不足", "data" => $data],
            "NO_POST" => ["status" => 4, "msg" => "非法请求", "data" => $data],
            "USER_ERROR" => ["status" => 5, "msg" => "用户名已存在", "data" => $data],
            "CAOZUO_ERROR" => ["status" => 6, "msg" => "操作失败", "data" => $data],
            "ROLE_ERROR" => ["status" => 7, "msg" => "该角色已经存在", "data" => $data]
        ];
        return $maps[$key];
    }
}